<?php

namespace app\api\controller\fishing;

use app\admin\model\FishingAcc;
use app\admin\model\FishingFishpond;
use app\api\controller\Utils;
use app\common\controller\Api;
use think\Request;

/**
 * 天天去钓鱼用户相关接口
 */
class Fishingaccs extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public $skill;
    public $control_version = '0.0.0';
    public $new_version = '1.2.1';

    public function __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin:*');
        parent::__construct($request);
        $this->skill = new Skill();
    }

    // 登录接口
    public function login()
    {
        $util = new Utils();
        $gh_id = $this->request->post('gh_id');
        $code = $this->request->post('code');

        $openid = $util->get_openid($gh_id, $code);

        if ($openid) {
            if (!FishingAcc::get(['openid' => $openid])) {
                $a = new FishingAcc;
                $a->openid = $openid;
                $a->max_score = 0;
                $a->money = 300;
                $a->fishpond_count = 10;
                $a->fishpond_rate = 0;
                $a->guidance = '0_0_0';
                $a->save();
            }

            $this->result('login', $openid);
        }

        $this->error('error');
    }

    // 获取用户信息
    public function getUserInfo()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $nick_name = $this->request->post('nick_name');
        $avatar_url = $this->request->post('avatar_url');
        $version = $this->request->post('version') ?: '';

        // 更新头像昵称
        $acc = FishingAcc::get(['openid' => $openid]);
        if ($nick_name) {
            $acc->nick_name = $nick_name;
        }
        if ($avatar_url) {
            $acc->avatar_url = $avatar_url;
        }
        $acc->save();

        $data['max_score'] = $acc->max_score;
        $data['money'] = $acc->money;
        $data['last_logout_time'] = $acc->last_logout_time;
        $data['fishpond_count'] = $acc->fishpond_count;
        $data['fishpond_income'] = $this->get_fishpond_income($openid, $acc->fishpond_rate);
        $data['fishpond_rate'] = $acc->fishpond_rate;

        $depth_level = intval($acc->depth_level);
        $count_level = intval($acc->count_level);
        $offline_level = intval($acc->offline_level);

        $data['count']['level'] = $count_level;
        $data['count']['num'] = $this->skill->count_num($count_level);
        $data['count']['next_pay'] = $this->skill->count_next_pay($count_level);

        $data['depth']['level'] = $depth_level;
        $data['depth']['num'] = $this->skill->depth_num($depth_level);
        $data['depth']['next_pay'] = $this->skill->depth_next_pay($depth_level);

        $data['offline']['level'] = $offline_level;
        $data['offline']['num'] = $this->skill->offline_num($offline_level);
        $data['offline']['next_pay'] = $this->skill->offline_next_pay($offline_level);

        $data['offline_money'] = $this->get_offline_money($acc);

        $data['guidance'] = $this->get_guidance_info($acc->guidance);

        $data['multiple'] = 2; // 初始倍数
        $data['isVideo'] = true; // 是否走广告
        $data['navigate_appid'] = 'wx7c0beb2fc324a64e';

        // 审核控制
        $data['flag'] = false;
        if ($version == $this->control_version) {
            $data['flag'] = true;
        }

        $this->result('data', $data);
    }

    // 获取鱼塘每小时收益
    public function get_fishpond_income($openid, $fishpond_rate)
    {
        return floor(FishingFishpond::where(['openid' => $openid])->sum('price') * 2 * (1 + $fishpond_rate));
    }

    // 获取跳转小程序
    public function get_navigate_list()
    {
        $version = $this->request->post('version') ?: '';

        $list = array(
            array(
                'name' => '一起飞飞飞',
                'appid' => 'wxe42ac5dd0f116b26',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fly.jpg'
            ),
            array(
                'name' => '重名查询',
                'appid' => 'wx3093c5ec75efcd7b',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/cmcx2.jpg'
            ),
            array(
                'name' => '蹦蹦Jump',
                'appid' => 'wxfc2e85ad6e4c1691',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/bbjump.jpg'
            ),
            array(
                'name' => '嗨爆小片',
                'appid' => 'wx38e4080448c98296',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/hbxp.jpg'
            ),
            array(
                'name' => '随你搞',
                'appid' => 'wx07cac980fde4202a',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/sng.jpg'
            ),
            array(
                'name' => '魔盘',
                'appid' => 'wx1ce4c0d710fcec54',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mp.jpg'
            ),
            array(
                'name' => '阅享每日一文',
                'appid' => 'wx9a21a5ca27f8cbc5',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mryw2.jpg'
            ),
            array(
                'name' => '天天广场舞教学',
                'appid' => 'wx77a9968ac2c2d9e7',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/ttgcwjx.jpg'
            ),
        );

        if ($version == $this->new_version) {
            $item = array(
                'name' => '天天爱开炮',
                'appid' => 'wx004a0a1ac772f7b0',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/ttakp.jpg'
            );
        } else {
            $item = array(
                'name' => '精品盒子',
                'appid' => 'wx7c0beb2fc324a64e',
                'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mryw.jpg'
            );
        }
        array_unshift($list, $item);

        $this->result('navigate', $list);
    }

    // 升级技能接口
    public function update_skill()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $type = $this->request->post('type');
        $acc = FishingAcc::get(['openid' => $openid]);

        switch ($type) {
            case 'count':
                $this->update_fish_count($acc);
                break;
            case 'depth':
                $this->update_fish_depth($acc);
                break;
            case 'offline':
                $this->update_fish_offline($acc);
                break;
        }
    }

    // 升级钓鱼数量
    public function update_fish_count($acc)
    {
        $level = intval($acc->count_level);
        $next_pay = $this->skill->count_next_pay($level);
        if ($acc->money < $next_pay) {
            $this->error('10001');
        }
        $acc->money = $acc->money - $next_pay;
        $acc->count_level = $level + 1;
        $acc->save();
        $data['money'] = $acc->money;
        $data['count']['level'] = $acc->count_level;
        $data['count']['num'] = $this->skill->count_num($acc->count_level);
        $data['count']['next_pay'] = $this->skill->count_next_pay($acc->count_level);
        $data['type'] = 'count';
        $this->result('ok', $data);
    }

    // 升级钓鱼深度
    public function update_fish_depth($acc)
    {
        $level = intval($acc->depth_level);
        $now_depth = $this->skill->depth_num($level);
        if ($now_depth >= 700) {
            $this->error('10001');
        }
        $next_pay = $this->skill->depth_next_pay($level);
        if ($acc->money < $next_pay) {
            $this->error('10001');
        }
        $acc->money = $acc->money - $next_pay;
        $acc->depth_level = $level + 1;
        $acc->save();
        $data['money'] = $acc->money;
        $data['depth']['level'] = $acc->depth_level;
        $data['depth']['num'] = $this->skill->depth_num($acc->depth_level);
        $data['depth']['next_pay'] = $this->skill->depth_next_pay($acc->depth_level);
        $data['type'] = 'depth';
        $this->result('ok', $data);
    }

    // 升级离线收益
    public function update_fish_offline($acc)
    {
        $level = intval($acc->offline_level);
        $next_pay = $this->skill->offline_next_pay($level);
        if ($acc->money < $next_pay) {
            $this->error('10001');
        }
        $acc->money = $acc->money - $next_pay;
        $acc->offline_level = $level + 1;
        $acc->save();
        $data['money'] = $acc->money;
        $data['offline']['level'] = $acc->offline_level;
        $data['offline']['num'] = $this->skill->offline_num($acc->offline_level);
        $data['offline']['next_pay'] = $this->skill->offline_next_pay($acc->offline_level);
        $data['type'] = 'offline';
        $this->result('ok', $data);
    }

    // 计算离线收益
    public function get_offline_money($acc)
    {
        $money = 0;
        $logout_time = $acc->last_logout_time;
        if ($logout_time) {
            $offtime = floor((intval(time()) - $logout_time) / 3600);
            $money = $this->skill->offline_num($acc->offline_level) * $offtime;
        }

        return $money;
    }

    // 获取离线收益
    public function update_offline_money()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $multiple = $this->request->post('multiple');

        $acc = FishingAcc::get(['openid' => $openid]);

        $offline_money = $this->get_offline_money($acc);
        $acc->money += ($offline_money) * $multiple;
        $acc->save();
        $this->result('ok', $acc->money);
    }

    // 获取新手教程数据
    public function get_guidance_info($guidance)
    {
        $collected = explode('_', $guidance);

        return $data = array(
            1 => array(
                'guidance_1' => intval($collected[0]),
                'guidance_2' => intval($collected[1]),
                'guidance_3' => intval($collected[2]),
            )
        );
    }

    // 更新新手教程数据
    public function update_guidance()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $guidance = $this->request->post('guidance');

        $acc = FishingAcc::get(['openid' => $openid]);

        switch ($guidance) {
            case '0_0_0':
                $acc->guidance = '1_0_0';
                break;
            case '1_0_0':
                $acc->guidance = '1_1_0';
                break;
            case '1_1_0':
                $acc->guidance = '1_1_1';
                break;
        }

        $acc->save();

        $data = $this->get_guidance_info($acc->guidance);

        $this->result('ok', $data);
    }

    // 获取排行榜数据
    public function get_rank()
    {
        $page = $this->request->post('page');
        $type = $this->request->post('type');

        if ($type == '0' || $type == 0) {
            $field = 'max_score';
        }
        if ($type == '1' || $type == 1) {
            $field = 'depth_level';
        }

        $list = FishingAcc::limit(($page - 1) * 10, 10)->order($field, 'desc')->select();
        $rank_list = array();
        foreach ($list as $item) {
            $res['nick_name'] = $item->nick_name;
            $res['avatar_url'] = $item->avatar_url;
            if ($type == '0' || $type == 0) {
                $res['max_score'] = $item->max_score;
            }
            if ($type == '1' || $type == 1) {
                $res['max_score'] = $this->skill->depth_num($item->depth_level);
            }
            array_push($rank_list, $res);
        }
        $this->result('ok', $rank_list);

    }

    // 获取自己的排行数据
    public function get_self_rank()
    {
        $openid = $this->request->post('openid') ?: 'mnq';
        $type = $this->request->post('type');

        if ($type == '0' || $type == 0) {
            $field = 'max_score';
        }
        if ($type == '1' || $type == 1) {
            $field = 'depth_level';
        }

        $acc = FishingAcc::get(['openid' => $openid]);

        $list = FishingAcc::limit(0, 1000)->order($field, 'desc')->select();

        $data['rank_id'] = 0;

        foreach ($list as $key => $item) {
            if ($item->openid == $openid) {
                $data['rank_id'] = $key + 1;
            }
        }

        $data['nick_name'] = $acc->nick_name;
        $data['avatar_url'] = $acc->avatar_url;
        if ($type == '0' || $type == 0) {
            $data['max_score'] = $acc->max_score;
        }
        if ($type == '1' || $type == 1) {
            $data['max_score'] = $this->skill->depth_num($acc->depth_level);
        }

        $this->result('ok', $data);
    }
}
